﻿using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;
using ParangonBiz.Data;

namespace ParangonBiz.Tools
{
	public class MailHelper
	{
		public enum MailType
		{
			Donotreply,
			Support,
			AdminMail
		}

		public static void SendEmail(string sToAddress, bool bIsAdminCopy, string sEmailSubject, string sEmailBody)
		{
			MailMessage message = new MailMessage(new MailAddress(ConfigReader.DonotreplyMail),
												  new MailAddress(sToAddress))
									{
										Subject = sEmailSubject.Trim(),
										IsBodyHtml = true,
										Body = sEmailBody
									};

			if (bIsAdminCopy)
			{
				message.Bcc.Add(new MailAddress(ConfigReader.AdminMail));
			}

			SmtpClient client = new SmtpClient(ConfigReader.DefaultSmtpServer)
									{
										Credentials =
											new NetworkCredential(ConfigReader.DefaultSmtpAccount,
																  ConfigReader.DefaultSmtpPassword),
										Port = ConfigReader.DefaultSmtpPort,
										EnableSsl = true
									};

			client.Send(message);
		}

		public static void SendEmail(EmailTemplate etEmailTemplate, string sToAddress, bool bIsAdminCopy, string sEmailSubject, string[] sEmailBodyParams)
		{
			if (sEmailBodyParams == null)
				return;

			MailMessage message = new MailMessage(new MailAddress(ConfigReader.DonotreplyMail),
													new MailAddress(sToAddress))
			{
				Subject = sEmailSubject.Trim(),
				IsBodyHtml = true,
				Body = String.Format(etEmailTemplate.Content, sEmailBodyParams)
			};

			if (bIsAdminCopy)
			{
				message.Bcc.Add(new MailAddress(ConfigReader.AdminMail));
			}

			if (etEmailTemplate.FileAttachements.Count > 0)
			{
				foreach (FileAttach emailTemplateAttach in etEmailTemplate.FileAttachements)
				{
					string sAttachmentPath =
						Path.Combine(HttpContext.Current.Server.MapPath(ConfigReader.EmailTemplateAttachmentsPath),
										emailTemplateAttach.DocumentName);

					message.Attachments.Add(new Attachment(sAttachmentPath));
				}
			}

			SmtpClient client = new SmtpClient(ConfigReader.DefaultSmtpServer)
			{
				Credentials = new NetworkCredential(ConfigReader.DefaultSmtpAccount, ConfigReader.DefaultSmtpPassword),
				Port = ConfigReader.DefaultSmtpPort,
				EnableSsl = true
			};
			client.Send(message);
		}

		public static void SendEmail(EmailTemplate etEmailTemplate, string sToAddress, bool bIsAdminCopy, string[] sEmailBodyParams)
		{
			SendEmail(etEmailTemplate, sToAddress, bIsAdminCopy, etEmailTemplate.Subject, sEmailBodyParams);
		}

		public static void SendEmailToSupport(string sSenderAddress, bool bIsSenderCopy, string[] sEmailBodyParams)
		{
			EmailTemplate etSupport = EmailTemplate.GetEmailTemlateByType((int)EnumHelper.EmailTemplateType.Support);
			if (etSupport == null)
				return;

			if (sEmailBodyParams != null)
			{
				MailMessage message = new MailMessage(new MailAddress(ConfigReader.DonotreplyMail),
													  new MailAddress(ConfigReader.AdminMail))
				{
					Subject = etSupport.Subject,
					IsBodyHtml = true,
					Body = string.Format(etSupport.Content, sEmailBodyParams)
				};

				if (bIsSenderCopy)
				{
					message.Bcc.Add(new MailAddress(sSenderAddress));
				}

				SmtpClient client = new SmtpClient(ConfigReader.DefaultSmtpServer)
				{
					Credentials =
						new NetworkCredential(ConfigReader.DefaultSmtpAccount,
											  ConfigReader.DefaultSmtpPassword),
					Port = ConfigReader.DefaultSmtpPort,
					EnableSsl = true
				};
				client.Send(message);
			}
		}

		public static void SendEmailToAdmin(string sEmailSubject, string sEmailBody)
		{
			MailMessage message = new MailMessage(new MailAddress(ConfigReader.DonotreplyMail),
												  new MailAddress(ConfigReader.AdminMail))
									{
										Subject = sEmailSubject,
										IsBodyHtml = true,
										Body = sEmailBody
									};

			SmtpClient client = new SmtpClient(ConfigReader.DefaultSmtpServer)
									{
										Credentials =
											new NetworkCredential(ConfigReader.DefaultSmtpAccount,
																  ConfigReader.DefaultSmtpPassword),
										Port = ConfigReader.DefaultSmtpPort,
										EnableSsl = true
									};
			client.Send(message);

		}

		///// <summary>
		///// Send email with error description to administrator
		///// </summary>
		///// <param name="PageName"></param>
		///// <param name="PageAction"></param>
		///// <param name="ex"></param>
		//public static void SendEmail(string PageName, string PageAction, Exception ex)
		//{
		//    StringBuilder ErrorBuilder = new StringBuilder();
		//    ErrorBuilder.AppendFormat("Page: {0} <br />", PageName);
		//    ErrorBuilder.AppendFormat("Action: {0}<br />", PageAction);
		//    ErrorBuilder.AppendFormat("Message: {0} <br />", ex.Message);
		//    ErrorBuilder.AppendFormat("Source: {0} <br /><br />", ex.Source);
		//    ErrorBuilder.AppendFormat("Stack trace: {0} <br /><br />", ex.StackTrace);
		//    ErrorBuilder.AppendFormat("Error time: {0} <br /><br />", DateTime.Now.ToString());

		//    MailMessage message = new MailMessage(new MailAddress(ConfigReader.DonotreplyMail),
		//                                          new MailAddress(ConfigReader.AdminMail)) { Subject = "ReferralsUS Error", IsBodyHtml = true, Body = ErrorBuilder.ToString() };


		//    SmtpClient client = new SmtpClient("localhost");
		//    client.Send(message);
		//}
	}
}
